<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Information extends CCMIS_Controller
{
	public function __construct() {
		parent::__construct();
        $this->load->model('model_facultyinformation');
        $this->load->model('model_facultymember');
	}

    public function index()
    {
        $this->view();
    }

    public function view()
    {
        $topics = $this->model_facultyinformation->getAll();
        $topicsNew = array();
        foreach($topics as $topic)
        {
            $topicNew = $topic;
            $topicNew['UploaderName'] = $this->model_facultymember->getNameAppendedById($topic['UploaderID']);
            $topicNew['Content'] = nl2br($topic['Content']);

            array_push($topicsNew, $topicNew);
        }
        $data = array(
           'page_title'  => 'Information',
           'topics' => $topicsNew
        );
    	$this->setup_view('fmis/information/view', $data);
    }

    public function update($id)
    {
        $title = $this->input->post('textarea_title');
        $content = $this->input->post('textarea_content');
        $date = date('Y/m/d h:i:s', time());
        $uploaderID = $this->session->userdata('user_id');



        try
        {
            $this->model_facultyinformation->edit($id, $title, $content, $uploaderID, $date);
            $data = array(
                'success' => true,
                'message' => '',
                'id'=>$id,
                'title'=>$title,
                'content'=>$content,
                'date'=> $date,
            );

        }catch(ErrorException $err){
            $data = array(
                'success' => false,
                'message' => $err->getMessage(),
                'id'=>$id,
                'title'=>$title,
                'content'=>$content,
                'date'=> $date,
            );
        }

        echo json_encode($data);
    }

    public function addTopic()
    {
        $title = $this->input->post('addNewTopicTitle');
        $content = $this->input->post('addNewTopicContent');
        $date = date('Y/m/d h:i:s', time());
        $uploaderID = $this->session->userdata('user_id');

        $this->model_facultyinformation->add($title, $content, $uploaderID, $date);

        $data = array(
            'success' => true,
            'message' => '',
            'title'=>$title,
            'content'=>$content,
            'date'=> $date,
        );

        echo json_encode($data);

    }

    public function deleteTopic($id)
    {
        $this->model_facultyinformation->deleteById($id);

        $data = array(
            'success' => true,
        );

        echo json_encode($data);
    }
}

/* End of file information.php */
/* Location: ./application/controllers/information.php */